﻿#pragma once

#include <GraphicsBaseItem.h>


class GraphicsImageItem;
class GraphicsScene;

// box数据结构体
struct GraphicsBoxItemData
{
	QVector<double> imageTransform; // image旋转矩阵
	QVector<double> boxTransform; // box旋转矩阵
	QSize boxSize; // box尺寸
	QSize imageSize; // image尺寸
	QImage image; // image图片
	friend QDebug operator<<(QDebug dbg, const GraphicsBoxItemData& item) {
		dbg.nospace() << "boxSize:" << item.boxSize
			<< ",imageSize:" << item.imageSize
			<< ",boxTransform:" << item.boxTransform
			<< ",imageTransform:" << item.imageTransform;
		return dbg;
	}
};

class GraphicsBoxItem : public GraphicsBaseItem
{
	Q_OBJECT
public:
	enum EXpandMode {
		FIX_1_1_Enum,//1:1
		FIX_3_4_Enum,//3:4
		FIX_4_3_Enum,//4:3
		FIX_9_16_Enum,//9:16
		FIX_16_9_Enum,//16:9
		FREE_Enum//自由
	};
	Q_ENUM(EXpandMode);

	GraphicsBoxItem(GraphicsScene* scene = nullptr, int width = 800, int height = 800);

	~GraphicsBoxItem();

	QSize setMode(const QSize& size, EXpandMode mode);

	void setImage(const QImage& image);

	virtual void initGraphics()override;

	GraphicsBoxItemData getBoxData();

	// 适应屏幕
	void fitToScreen();

	qreal getFitToScreenFactor(const QSizeF& boxRealsize);

	void setImageSelected();

	void setMaskVisable(bool visable);

	// 测试代码
	void test1();
	void test2(double scale);
	void test3();
	void test4();
	void test5();
signals:
	void sigModeChanged(EXpandMode mode);
public slots:
	void onMaskChanged(const QImage& mask);
private:
	QRectF getCenterBoxRect();

	void setFIX_1_1_Mode(const QSize& boxSize);

	void setFIX_3_4_Mode(const QSize& boxSize);

	void setFIX_4_3_Mode(const QSize& boxSize);

	void setFIX_9_16_Mode(const QSize& boxSize);

	void setFIX_16_9_Mode(const QSize& boxSize);

	void setFREE_Mode(const QSize& boxSize);

	void init();

	QVector<double> getImageTransform();

	QVector<double> getBoxTransform();

	void createImageItem();

	void createBackgroundImage();

	QMap<GraphicsBaseItem*, GraphicsItemInfo> getChildsPosMap();

	// 获取Image最小实际尺寸
	QSizeF getImageRealMinSize();

	void fitImageToSize();
protected:
	virtual void scaleGraphics(GraphicsPinkItemType type, const QPointF& delta)override;

	virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr)override;

	virtual void operationDone(GraphicsPinkItemType type, const QPointF& mousePoint)override;
private:
	QImage m_bcImage;
	GraphicsImageItem* m_imageItem = nullptr;
	EXpandMode m_mode = EXpandMode::FIX_1_1_Enum;
	// image到box的边距
	const int m_imagePadding = 50;
	// 中心虚线框尺寸
	const QSizeF m_centerBox = QSizeF(440, 440);
	// 是否显示虚线框
	bool m_isVisabledCenterBox = false;
};

using BoxGraphicsMode = GraphicsBoxItem::EXpandMode;
